<template>
	<view class=''>
		<radio-group name="pay_type" @change="pay_type_change">
			<block v-for="item in payments" :key="item.code">
				<view class="pay_item" v-if="!(type == 2 && item.code == 'balancepay')">
					<image :src="item.icon" mode="aspectFill" class="pay_icon"></image>
					<view class="" style="flex: 1;">
						<view class="" v-if="item.name==='余额支付'">
							{{ item.name}} ({{userBalance}})
						</view>
						<!--  v-if="item.name==='统统支付'" -->
						<view class="" v-else>
							{{ item.name}}
						</view>


						<!-- <view class="emo">{{ item.memo }}</view> -->
					</view>
					<radio :value="item.code" :checked="current_code==item.code" />
				</view>

				<view class="tishi" v-if="item.code == 'offlinepay' && current_code =='offlinepay'">
					请在扫码后在备注栏填写您的手机号码。以免影响您的到账！
				</view>
				<view class="input_box" v-if="item.code == 'offlinepay' && current_code =='offlinepay'">
					<view class="input_img_box">
						<image :src="shoukuan" mode="widthFix" @tap="show_img(shoukuan)" class="input_img"></image>
						收款二维码
					</view>
					<view class="input_img_box">
						<image src="/static/user/add_btn.png" v-if="!pingzheng" @tap="upload" mode="widthFix"
							class="input_img"></image>
						<image :src="pingzheng" v-else @tap="upload" mode="widthFix" class="input_img">
						</image>
						付款凭证
					</view>
				</view>
			</block>
		</radio-group>
		<!-- 	<view class="" @click="aa()">
			支付宝
			
		</view> -->
	</view>
</template>
<style>
	.tishi {
		padding: 0 10rpx;
		font-size: 35rpx;
		font-weight: 600;
		line-height: 80rpx;
		color: red;
	}

	.input_img {
		width: 250rpx;
		height: 250rpx;
	}

	.input_img_box {
		width: 250rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		line-height: 2;

	}

	.input_box {
		width: 100%;
		padding: 30rpx 40rpx;

		border: 1rpx solid #d9d9d9;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 28rpx;
		color: #333;
		margin-bottom: 30rpx;
		box-sizing: border-box;
		border-radius: 30rpx;
	}

	.emo {
		font-size: 24rpx;
		color: #f0d6b3;
	}

	.pay_icon {
		width: 50rpx;
		height: 50rpx;
		margin-right: 30rpx;

	}

	.pay_item {
		width: 100%;
		padding: 0 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 28rpx;
		color: #333;
		border-bottom: 1rpx solid #f5f5f5;
		/* background-color: #fff; */
		/* box-shadow: 0rpx -5rpx 10rpx 1rpx rgba(0, 0, 0, 0.16); */
		height: 100rpx;
		/* margin-bottom: 30rpx; */
		box-sizing: border-box;
		border-radius: 20rpx;
	}
</style>


<script>
	import {
		baseUrl
	} from '@/config/config.js'
	import {
		apiBaseUrl
	} from '@/config/config.js'
	export default {
		props: {
			//数量
			vModelValue: {
				type: Number,
				default () {
					return 1 //数量默认
				}
			},
			// 如果是商品订单此参数必须
			orderId: {
				type: String,
				default () {
					return ''
				}
			},
			// 如果是充值订单此参数必须
			recharge: {
				type: Number,
				default () {
					return 0
				}
			},
			// 用户id
			uid: {
				type: Number,
				default () {
					return 0
				}
			},
			// 订单类型
			type: {
				type: Number,
				default () {
					return 1
				}
			},

			sell_sn: {
				sell_sn: String,
				default () {
					return ''
				}
			},
			//支付类型
			pay_type: {
				pay_type: String,
				default () {
					return ''
				}
			},
			goods_code: {
				goods_code: String,
				default () {
					return ''
				}
			},
			
		},
		data() {
			return {
				userBalance: "",
				payments: [],
				openid: '',
				vModelValue: 1,
				popShow: false,
				payStatus: true,

				shoukuan: '',
				pingzheng: '',
				current_code: '',
				goods_code: '',
				sell_sn:''
			}
		},
		mounted() {
			this.getPayments()
			this.getUserInfo()
		},
		methods: {
			aa() {
				uni.requestPayment({
					provider: 'alipay',
					orderInfo: 'orderInfo', //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
					success: function(res) {
						console.log('success:' + JSON.stringify(res));
					},
					fail: function(err) {
						console.log('fail:' + JSON.stringify(err));
					}
				});
			},
			// 获取用户信息
			getUserInfo() {
				this.$api.userInfo({}, res => {
					if (res.status) {
						this.userInfo = res.data;
						this.userBalance = res.data.balance;
						console.log("---", this.userInfo)
					} else {
						this.$common.errorToShow(res.msg);
					}
				});
			},
			show_img: function(img) {
				uni.previewImage({
					urls: [img]
				})
			},
			upload: function() {
				this.$api.uploadFiles(res => {
					if (res.status) {
						this.pingzheng = res.data.url

						this.$emit('change_img', this.pingzheng)
					}
				})
			},


			pay_type_change: function(e) {
				console.log(e)
				this.$emit('change_type', e.detail.value)
				this.current_code = e.detail.value;
				if (e.detail.value == 'offlinepay') {
					let data = {
						code: 'offlinepay'
					}
					this.$api.getPaymentInfo(data, res => {
						if (res.status) {
							this.shoukuan = res.data;

						}
					})
				}
			},

			// 获取可用支付方式列表
			getPayments() {
				this.$api.paymentList({
					payment_code: this.pay_type,
					payment_type: this.type
				}, res => {
					if (res.status) {
						this.payments = this.formatPayments(res.data)
						console.log("res.data--", res.data)
						this.current_code = this.payments[0].code
						this.$emit('change_type', this.current_code)

						if (this.current_code == 'offlinepay') {
							let data = {
								code: 'offlinepay'
							}
							this.$api.getPaymentInfo(data, res => {
								if (res.status) {
									this.shoukuan = res.data;

								}
							})
						}
					}
				})
			},
			// 支付方式处理
			formatPayments(payments) {
				// h5支付并且是在微信浏览器内 过滤支付宝支付
				if (this.$common.isWeiXinBrowser()) {
					payments = payments.filter(item => item.code !== 'alipay')
				}

				// 如果是充值订单 过滤余额支付 过滤非线上支付方式
				if (this.type === 2) {
					payments = payments.filter(
						item => item.code !== 'balancepay' || item.is_online === 1
					)
				}

				// 设置logo图片
				payments.forEach(item => {
					this.$set(item, 'icon', '/static/image/' + item.code + '.png')
				})

				return payments
			},
			checkWXJSBridge(data) {
				let that = this
				let interval = setInterval(() => {
					if (typeof window.WeixinJSBridge != 'undefined') {
						clearTimeout(interval)
						that.onBridgeReady(data)
					}
				}, 200)
			},
			onBridgeReady(data) {
				var _this = this
				window.WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						appId: data.appid, // 公众号名称，由商户传入
						timeStamp: data.timeStamp, // 时间戳，自1970年以来的秒数
						nonceStr: data.nonceStr, // 随机串
						package: data.package,
						signType: data.signType, // 微信签名方式：
						paySign: data.paySign // 微信签名
					},
					function(res) {
						if (res.err_msg === 'get_brand_wcpay_request:ok') {
							_this.$common.successToShow('支付成功')
						} else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
							_this.$common.errorToShow('取消支付')
						} else {
							_this.$common.errorToShow('支付失败')
						}
						setTimeout(() => {
							_this.$common.redirectTo(
								'/pages/payment/result?id=' + data.payment_id
							)
						}, 1000)
					}
				)
			},
			// 用户点击支付方式处理
			toPayHandler() {
				this.popShow = true;
				if (!this.pay_type) {
					this.$common.errorToShow('请选择支付方式');
					return
				}

				let code = this.pay_type
				
					let _this = this
					
				if(code==='huifupay'){
					console.log(" _this.vModelValue--", _this.vModelValue)
					var data = {
						sell_sn:_this.sell_sn,
						goods_code:_this.goods_code,
						payment_code: this.pay_type,
						payment_type: this.pay_type,
						num: _this.vModelValue
					}
					console.log("汇付")
				}else{
					var data = {
						sell_sn:_this.sell_sn,
						goods_code:_this.goods_code,
						payment_code: this.pay_type,
						payment_type: this.type,
						num: _this.vModelValue
					}
						console.log("不是汇付")
				}
			
				 
				data['ids'] = (this.type == 1 || this.type == 5 || this.type == 6 || this.type == 7 || this.type == 8) ?
					this.orderId : this.uid
				switch (code) {
					case 'adapaya':

						data['params'] = {
							trade_type: 'WAP',
							return_url: baseUrl + 'wap/pages/payment/result',
						}
						if (this.type == 2 && this.recharge) {
							data['params'].money = this.recharge
						}
						if (this.type == 9 && this.recharge) {
							data['params'].money = this.recharge
						}
						this.$api.pay(data, res => {
							console.log(res.data);
							if (res.status) {
								window.location = res.data;
							} else {
								this.$common.errorToShow(res.msg)
							}
						})
						break;
					case 'offlinepay':
						data['params'] = {
							trade_type: 'WAP',
							return_url: baseUrl + 'wap/pages/payment/result',
							image: this.pingzheng
						}
						if (this.type == 2 && this.recharge) {
							data['params'].money = this.recharge
						}
						this.$api.pay(data, res => {
							if (res.status) {
								this.$common.successToShow(res.msg, res => {
									uni.navigateBack()
								})
							} else {
								this.$common.errorToShow(res.msg)
							}
						})
						break
					case 'sandpay':
						data['params'] = {
							trade_type: 'WAP',
							return_url: baseUrl + 'wap/pages/payment/result',
						}
						if (this.type == 2 && this.recharge) {
							data['params'].money = this.recharge
						}
						this.$api.pay(data, res => {
							console.log(res.data);
							if (res.status) {
								window.location = res.data;
							} else {
								this.$common.errorToShow(res.msg)
							}
						})
						break;
					case 'sumpay':
						data['params'] = {
							trade_type: 'WAP',
							return_url: apiBaseUrl + 'wap/pages/payment/result',
						}


						if (this.type == 2 && this.recharge) {
							data['params'].money = this.recharge
						}
						this.$api.pay(data, res => {
							if (res.status) {
								// #ifdef H5
								window.location.href = res.data;
								// #endif
								// #ifdef APP-PLUS
								plus.runtime.openURL(res.data);
								// #endif
							} else {
								this.$common.errorToShow(res.msg)
							}
						})
						break;
					case 'joinpay':
						data['params'] = {
							trade_type: 'WAP',
							return_url: baseUrl + 'wap/pages/payment/result',
						}
						if (this.type == 2 && this.recharge) {
							data['params'].money = this.recharge
						}

						this.$api.pay(data, res => {
							if (res.status) {
								console.log("————————", res.data);
								var parser = new DOMParser();
								var resXML = parser.parseFromString(res.data.rc_Result, 'text/html');
								var tempForm = resXML.querySelector('form');

								document.body.appendChild(tempForm)

								tempForm.addEventListener('submit', function() {}, false)
								document.body.appendChild(tempForm)
								tempForm.dispatchEvent(new Event('submit'))
								tempForm.submit()
								document.body.removeChild(tempForm)

							} else {
								this.$common.errorToShow(res.msg)
							}
						})
						break;
					case 'alipay':
						/**
						 * 支付宝支付需要模拟GET提交数据
						 */
						if (this.type == 1 && this.orderId) {
							data['params'] = {
								trade_type: 'WAP',
								return_url: baseUrl +
									'wap/pages/payment/result'
							}
						} else if (this.type == 2 && this.recharge) {
							data['params'] = {
								money: this.recharge,
								return_url: baseUrl + 'wap/pages/payment/result'
							}
						} else if ((this.type == 5 || this.type == 6) && this.recharge) {
							data['params'] = {
								formid: this.orderId
							}
						} else if (this.type == 7 || this.type == 8) {
							data['params'] = {
								trade_type: 'WAP',
								return_url: baseUrl +
									'wap/pages/payment/result'
							}
						}

						this.$api.pay(data, res => {
							if (res.status) {
								const url = res.data.url
								const data = res.data.data

								// 模拟GET提交
								let tempForm = document.createElement('form')
								tempForm.id = 'aliPay'
								tempForm.methods = 'post'
								tempForm.action = url
								tempForm.target = '_self'
								let input = []
								for (let k in data) {
									input[k] = document.createElement('input')
									input[k].type = 'hidden'
									input[k].name = k
									input[k].value = data[k]
									tempForm.appendChild(input[k])
								}
								tempForm.addEventListener('submit', function() {}, false)
								document.body.appendChild(tempForm)
								tempForm.dispatchEvent(new Event('submit'))
								tempForm.submit()
								document.body.removeChild(tempForm)
							} else {
								this.$common.errorToShow(res.msg)
							}
						})
						break
					case 'wechatpay':
						if (this.payStatus == false) {
							return
						}
						this.payStatus = false
						let _this = this
						/**
						 * 微信支付有两种
						 * 判断是否在微信浏览器
						 * 	微信jsapi支付
						 */
						let isWeiXin = this.$common.isWeiXinBrowser()

						if (isWeiXin) {
							var transitUrl =
								baseUrl +
								'wap/pages/payment/auth?order_id=' +
								this.orderId +
								'&type=' +
								this.type;

							if (this.type == 1 && this.orderId) {
								// 微信jsapi支付
								// if (this.openid) {
								//   data['params'] = {
								//     trade_type: 'JSAPI_OFFICIAL',
								//     openid: this.openid
								//   }
								// } else {
								//   data['params'] = {
								//     trade_type: 'JSAPI_OFFICIAL',
								//     url: window.location.href
								//   }
								// }
								data['params'] = {
									trade_type: 'JSAPI_OFFICIAL',
									url: transitUrl
								}
							} else if (this.type == 2 && this.recharge) {
								data['params'] = {
									trade_type: 'JSAPI_OFFICIAL',
									money: this.recharge,
									url: transitUrl + '&uid=' + this.uid + '&money=' + this.recharge
								}
								// if (this.openid) {
								//   data['params'] = {
								//     money: this.recharge,
								//     openid: this.openid
								//   }
								// } else {
								//   data['params'] = {
								//     money: this.recharge,
								//     url: window.location.href
								//   }
								// }
							} else if ((this.type == 5 || this.type == 6) && this.recharge) {
								data['params'] = {
									formid: this.orderId,
									trade_type: 'JSAPI_OFFICIAL',
									url: transitUrl
								}
							} else if (this.type == 7 || this.type == 8) {
								data['params'] = {
									trade_type: 'JSAPI_OFFICIAL',
									url: transitUrl
								}
							}
							this.$api.pay(data, res => {
								if (!res.status && res.data == '10066') {
									window.location.href = res.msg
									return;
								}
								if (!res.status) {
									this.$common.errorToShow(res.msg);
									return;
								}
								const data = res.data
								this.checkWXJSBridge(data)
							})
						} else {
							// 微信 H5支付
							if (this.type == 1 && this.orderId) {
								data['params'] = {
									trade_type: 'MWEB',
									return_url: baseUrl +
										'wap/pages/payment/result'
								}
							} else if (this.type == 2 && this.recharge) {
								data['params'] = {
									trade_type: 'MWEB',
									money: this.recharge,
									return_url: baseUrl + 'wap/pages/payment/result'
								}
							} else if ((this.type == 5 || this.type == 6) && this.recharge) {
								data['params'] = {
									formid: this.orderId,
									trade_type: 'MWEB',
									return_url: baseUrl + 'wap/pages/payment/result'
								}
							} else if (this.type == 7 || this.type == 8) {
								data['params'] = {
									trade_type: 'MWEB',
									return_url: baseUrl +
										'wap/pages/payment/result'
								}
							}
							// 微信h5支付
							this.$api.pay(data, res => {
								_this.payStatus = true
								if (res.status) {
									location.href = res.data.mweb_url
								} else {
									this.$common.errorToShow(res.msg)
								}
							})
						}
						break
					case 'balancepay':
						/**
						 *  用户余额支付
						 *
						 */
						if ((this.type == 5 || this.type == 6) && this.recharge) {
							data['params'] = {
								formid: this.orderId
							}
						}

						console.log("this.orderId--", this.orderId)
						console.log("this.vModelValue-", data)
						this.$api.pay(data, res => {
							console.log("res00", res)
							if (res.status) {
								this.$common.redirectTo(
									'/pages/payment/result?id=' + res.data.payment_id
								)
							} else {
								this.$common.errorToShow(res.msg)
							}
						})
						break
					case 'huifupay':
			
					   console.log("我传的参数--",data)
					   this.$api.pay(data, res => {
						console.log("huifupay--获取-------", res)
						if (res.status===1) {
							// uni.navigateTo({            
							// 	url:'/pages/user/webview/webview?url='+res.data.pay_url 
							// }) pay_url  redirect_url
				  window.location.href =res.data.redirect_url 
						} else {
							//	this.$refs.chaochu.open()
							// console.log("res.data.once_fen-",res.data.once_fen)
							// if(res.data.once_fen){
							// 	this.vModelValue=res.data.once_fen
							// }
							this.$common.errorToShow(res.msg)
						}
					})
						break
					case 'offline':
						/**
						 * 线下支付
						 */
						this.$common.modelShow(
							'线下支付说明',
							'请联系客服进行线下支付',
							() => {},
							false,
							'取消',
							'确定'
						)
						break
				}
			},
			// 支付中显示隐藏
			popBtn() {
				this.popShow = false
			}
		}
	}
</script>